package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface SetmealDao_lzl {
    /*
    * 添加套餐管理
    * */
    @Insert("insert into t_setmeal values(#{id},#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()",keyProperty = "id",before = false,resultType = Integer.class)
    void add(Setmeal setmeal);

    /*
    * 添加到中间表关联表
    * */
    @Insert("insert into t_setmeal_checkgroup values(#{setmeal_id},#{checkgroup_id})")
    void addsetmealAndcheckgroup(Map<String, Integer> map);
    /*
    * 根据条件分页查询
    * */
    @Select("<script> select * from t_setmeal"+
                "<where>"+
                    "<if test='value!=null and value.length>0'> and code=#{value} </if>"+
                    "<if test='value!=null and value.length>0'> or name=#{value} </if>"+
                    "<if test='value!=null and value.length>0'> or helpCode=#{value} </if>"+
                "</where>"+
            "</script>"
    )
    Page<Setmeal> findPage(String queryString);
    /*
     * 根据id，查询setmeal对象，并进行返回
     * */
    @Select("select * from t_setmeal where id=#{id}")
    Setmeal findByid(Integer id);
    /*
     * 根据SetmealId 查询CheckgroupIds
     * */
    @Select("select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id}")
    List<Integer> findCheckgroupIdsBySetmealId(Integer id);
    /*
     * 修改套餐管理
     * */
    @Update("<script> update t_setmeal"+
                "<set>"+
                    "<if test='name!=null'> name=#{name}, </if>"+
                    "<if test='code!=null'> code=#{code}, </if>"+
                    "<if test='helpCode!=null'> helpCode=#{helpCode}, </if>"+
                    "<if test='sex!=null'> sex=#{sex}, </if>"+
                    "<if test='age!=null'> age=#{age}, </if>"+
                    "<if test='price!=null'> price=#{price}, </if>"+
                    "<if test='remark!=null'> remark=#{remark}, </if>"+
                    "<if test='attention!=null'> attention=#{attention}, </if>"+
                    "<if test='img!=null'> img=#{img}, </if>"+
                "</set>"+
                "where id = #{id}"+
            "</script>"
    )
    void update(Setmeal setmeal);
    /*
    * 根據setmealid，刪除checkGroupids
    * */
    @Delete("delete from t_setmeal_checkgroup where setmeal_id=#{id}")
    void deleteCheckGroupIdsBySetmealId(Integer id);

    /*
    * 根据id 删除
    * */
    @Delete("delete  from  t_setmeal where id=#{id}")
    void delete(Integer id);

    /*
    * 查詢所有套餐
    * */
    @Select("select * from t_setmeal")
    List<Setmeal> getSetmeal();


    /*
    * 根据id查詢setmeal，在跟id查询checkGroup
    * */
    @Select("select * from t_setmeal where id = #{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "code",column = "code"),
            @Result(property = "helpCode",column = "helpCode"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "age",column = "age"),
            @Result(property = "price",column = "price"),
            @Result(property = "remark",column = "remark"),
            @Result(property = "attention",column = "attention"),
            @Result(property = "img",column = "img"),
            @Result(
                    property = "checkGroups",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.itheima.dao.CheckGroupDao.findCheckGroupBySetmealId")
            )
    })
    Setmeal findById(Integer id);

    @Select("SELECT s.name name,COUNT(*) value FROM t_order o,t_setmeal s WHERE s.id=o.setmeal_id GROUP BY s.name")
    List<Map> getSetmealNameAndOrderCount();
}
